package com.project.hqshorttaxi.controller;

import com.github.pagehelper.PageHelper;
import com.project.hqshorttaxi.aop.TagLog;
import com.project.hqshorttaxi.dto.FinishSettingParam;
import com.project.hqshorttaxi.entity.FinishSetting;
import com.project.hqshorttaxi.entity.Login;
import com.project.hqshorttaxi.service.FinishSettingService;
import com.project.hqshorttaxi.service.LoginService;
import com.project.hqshorttaxi.util.PageInfo;
import com.project.hqshorttaxi.util.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/api/finishSetting/")
public class FinishSettingController {

    @Resource
    FinishSettingService finishSettingService;
    @Resource
    LoginService loginService;

    /**
     * 更新
     */
    @PostMapping("update")
    @TagLog(operation = 3, content = "更新营运结束时间")
    public RestResult update(@RequestHeader(value = "authorization") String token, @RequestBody FinishSetting param) {
        try {
            Login login = loginService.getByToken(token);
            if (login == null) {
                return RestResult.error("用户不存在");
            }
            FinishSetting entity = finishSettingService.getById(param.getId());
            if (entity == null) {
                return RestResult.error("该条数据不存在");
            } else {
                param.setUpdateTime(new Date());
                param.setUpdateUser(login.getUserName());
                finishSettingService.update(param);
                return RestResult.ok();
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 列表
     */
    @PostMapping("page")
    @TagLog(operation = 0, content = "查询营运结束时间")
    public RestResult page(@RequestBody FinishSettingParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        return RestResult.ok(new PageInfo<>(finishSettingService.list(param)));
    }

    /**
     * 根据ID获取
     */
    @GetMapping("{id}")
    @TagLog(operation = 0, content = "根据ID获取设备详细信息")
    public RestResult pageById(@PathVariable Integer id) {
        return RestResult.ok(finishSettingService.getById(id));
    }
}
